home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / PCASM2.ZIP / CHAP0-2.DOC < prev    next >
Text File  |  1990-08-04  |  40KB  |  980 lines

  1.  
  2.  
  3.  
  4.                                                                             xv
  5.  
  6.                                CHAPTER 0.2 - BASES 2 AND 16
  7.  
  8.  
  9.              I'm making the assumption that if you are along for the ride you
  10.              already know something about binary and hex numbers. This is a
  11.              review only.
  12.  
  13.  
  14.              BASE 2 AND BASE 16
  15.  
  16.              Base 2 (binary) allows only 0s and 1s. Base 16 (hexadecimal)
  17.              allows 0 - 9, and then makes up the next six numbers by using the
  18.              letters A - F. A = 10, B=11, C=12, D=13, E=14 and F=15. You can
  19.              directly translate a hex number to a binary number and a binary
  20.              number to a hex number. A group of four digits in binary is the
  21.              same as a single digit in hex. We'll get to that in a moment.
  22.  
  23.              The binary digits (BITS) are the powers of 2. The values of the
  24.              digits (in increasing order) are 1, 2, 4, 8, 16, 32, 64, 128, 256
  25.              and so on. 1 + 2 + 4 + 8 = 15, so the first four digits can
  26.              represent a hex number. This repeats itself every four binary
  27.              digits. Here are some numbers in binary, hex, and decimal
  28.  
  29.                  BINARY         HEX      DECIMAL
  30.  
  31.                  0100            4          4
  32.                  1111            F         15
  33.                  1010            A         10 
  34.                  0011            3          3
  35.  
  36.              Let's go from binary to hex. Here's a binary number.
  37.  
  38.                  0110011010101101
  39.  
  40.              To go from binary to hex, first divide the binary number up into
  41.              groups of four starting from the right.
  42.  
  43.                  0110 0110 1010 1101
  44.  
  45.              Now simply change each group into a hex number. 
  46.  
  47.                  0110 ->   4 + 2     ->   6
  48.                  0110 ->   4 + 2     ->   6
  49.                  1010 ->   8 + 2     ->   A
  50.                  1101 ->   8 + 4 + 1 ->   D
  51.  
  52.              and we have 66AD as the result. Similarly, to go from hex to
  53.              binary:
  54.  
  55.                  D39F
  56.  
  57.              change each hex digit into a set of four binary digits:
  58.  
  59.                  D = 13    ->   8 + 4 + 1 ->   1101
  60.  
  61.              ______________________
  62.  
  63.              The PC Assembler Tutor - Copyright (C) 1989 Chuck Nelson
  64.  
  65.  
  66.  
  67.  
  68.              The PC Assembler Tutor                                        xvi
  69.              ______________________
  70.  
  71.                  3         ->   2 + 1     ->   0011
  72.                  9         ->   8 + 1     ->   1001
  73.                  F = 15    ->   8+4+2+1   ->   1111
  74.  
  75.              and then put them all together:
  76.  
  77.                  1101001110011111 
  78.  
  79.              Of course, having 16 digits strung out like that makes it totally
  80.              unreadable, so in this book, if we are talking about a binary
  81.              number, it will always be separated every 4 digits for
  82.              clarity.{1} 
  83.  
  84.              All computers operate on binary data, so why do we use hex
  85.              numbers? Take a test. Copy these two binary numbers:
  86.  
  87.                  1011 1000 0110 1010 1001 0101 0111 1010
  88.                  0111 1100 0100 1100 0101 0110 1111 0011
  89.  
  90.              Now copy these two hex numbers:
  91.  
  92.                  B86A957A
  93.                  7C4C56F3
  94.  
  95.              As you can see, you recognize hex numbers faster and you make
  96.              fewer mistakes in transcription with hex numbers. 
  97.  
  98.  
  99.              ADDITION AND SUBTRACTION
  100.  
  101.              The rules for binary addition are easy:
  102.  
  103.                  0 + 0 = 0
  104.                  0 + 1 = 1
  105.                  1 + 0 = 1
  106.                  1 + 1 = 0  (carry 1 to the next digit left)
  107.  
  108.              similarly for binary subtraction:
  109.  
  110.                  0 - 0 = 0
  111.                  0 - 1 = 1  (borrow 1 from the next digit left)
  112.                  1 - 0 = 1
  113.                  1 - 1 = 0
  114.  
  115.  
  116.              On the 8086, you can have a 16 bit (binary digit) number
  117.              represent a number from 0 - 65535. 65535 + 1 = 0 (65536). For
  118.              binary numbers, the boundary is 65535/0. You count up or down
  119.              through that boundary. The 8086 is a mod 65536 machine. That
  120.              means the things that are equivalent to 35631 mod 65536 are:{2}
  121.  
  122.              ____________________
  123.  
  124.                 1. This will not be true of the actual assembler code, since
  125.              the assembler demands an unseparated number.
  126.  
  127.                 2. 35631 + 29905 = 65536.  -29905 = 35631 (mod 65536)
  128.  
  129.  
  130.  
  131.  
  132.              Chapter 0.2 - Binary and Hex Numbers                         xvii
  133.              ____________________________________
  134.  
  135.                  (3*65536 + 35631)        (3*65536 - 29905)
  136.                  (2*65536 + 35631)        (2*65536 - 29905)
  137.                  (1*65536 + 35631)        (1*65536 - 29905)
  138.                  (      0 + 35631)        (      0 - 29905)
  139.                  (-1*65536 + 35631)       (-1*65536 - 29905)
  140.                  (-2*65536 + 35631)       (-2*65536 - 29905)
  141.                  (-3*65536 + 35631)       (-3*65536 - 29905)
  142.  
  143.              The unsigned number 35631 and the signed number -29905 look the
  144.              same. They ARE the same. In all addition, they will operate in
  145.              the same fashion. The unsigned number will use CF (the carry
  146.              flag) and the signed number will use OF (the overflow flag). 
  147.  
  148.              On all 16 bit computers, 0-32767 is positive and 32768 - 65535 is
  149.              negative. Here's 32767 and 32768.
  150.  
  151.                  32767     0111 1111 1111 1111
  152.                  32768     1000 0000 0000 0000
  153.  
  154.              32768 and all numbers above it have the left bit 1. 32767 and all
  155.              numbers below it have the left bit 0. This is how to tell the
  156.              sign of a signed number. If the left bit is 0 it's positive and
  157.              if the left bit is 1 it's negative.
  158.  
  159.  
  160.              TWO'S COMPLEMENT
  161.  
  162.              In base 10 we had 10s complement to help us with negative
  163.              numbers. In base 2, we have 2s complememt.
  164.  
  165.                  0 = 65536 = 65535 + 1
  166.  
  167.              so we have:
  168.  
  169.                  1 0000 0000 0000 0000 =  1111 1111 1111 1111 + 1
  170.  
  171.              To get the negative of a number, we subtract:
  172.  
  173.                  -49 = 0 - 49 = 65536 - 49 = 65535 - 49 + 1
  174.  
  175.              (65536)  1111 1111 1111 1111 + 1
  176.                 (49)  0000 0000 0011 0001
  177.              result   1111 1111 1100 1110 + 1 -> 1111 1111 1100 1111  (-49)
  178.              ; - - - - -
  179.  
  180.              -21874
  181.              (65536)  1111 1111 1111 1111 + 1
  182.              (21874)  0101 0101 0101 0111
  183.              result   1010 1010 1010 1000 + 1 -> 1010 1010 1010 1001 (-21847)
  184.              ; - - - - -
  185.  
  186.              -11628
  187.              (65536)  1111 1111 1111 1111 + 1
  188.              (11628)  0010 1101 0110 1100
  189.              result   1101 0010 1001 0011 + 1 -> 1101 0010 1001 0100 (-11628)
  190.              ; - - - - -
  191.  
  192.  
  193.  
  194.  
  195.  
  196.              The PC Assembler Tutor                                      xviii
  197.              ______________________
  198.  
  199.              -1764
  200.              (65536)  1111 1111 1111 1111 + 1
  201.               (1764)  0000 0110 1110 0100 
  202.              result   1111 1001 0001 1011 + 1 -> 1111 1001 0001 1100 (-1764)
  203.              ; - - - - -
  204.  
  205.              Notice that since:
  206.  
  207.                  1 - 0 = 1
  208.                  1 - 1 = 0
  209.  
  210.              when you subtract from 1, you are simply switching the value of
  211.              the subtrahend (that's the number that you subtract).
  212.  
  213.                  1    ->   0
  214.                  0    ->   1
  215.  
  216.              1 becomes 0 and 0 becomes 1. You don't even have to think about
  217.              it. Just switch the 1s to 0s and switch the 0s to 1s, and then
  218.              add 1 at the end. Well do one more:
  219.  
  220.              -348
  221.              (65536) 1111 1111 1111 1111 + 1
  222.               (348)  0000 0001 0101 1100
  223.              result  1111 1110 1010 0011 + 1 ->  1111 1110 1010 0100 (-348)
  224.  
  225.              Now two more, this time without the crutch of having the top
  226.              number visible. Remember, even though you are subtracting, all
  227.              you really need to do is switch 1s to 0s and switch 0s to 1s, and
  228.              then add 1 at the end.
  229.  
  230.              -658
  231.  
  232.               (658)  0000 0010 1001 0010
  233.              result  1111 1101 0110 1101 + 1 -> 1111 1101 0110 1110 (-658)
  234.              ; - - - - -
  235.  
  236.              -31403
  237.  
  238.              (34103) 0111 1010 0100 0111
  239.              result  1000 0101 1011 1000 + 1 -> 1000 0101 1011 1001 (-31403)
  240.  
  241.  
  242.  
  243.              SIGN EXTENSION
  244.  
  245.              If you want to use larger numbers, it is possible to use multiple
  246.              words to represent them.{3}  The arithmetic will be done 16 bits
  247.              at a time, but by using the method described in Chapter 0.1, it
  248.              is possible to add and subtract numbers of any length. One normal
  249.              length is 32 bits. How do you convert a 16 bit to a 32 bit
  250.              number? If it is unsigned, simply put 0s to the left:
  251.  
  252.                0100 1100 1010 0111 ->  0000 0000 0000 0000 0100 1100 1010 0111
  253.              ____________________
  254.  
  255.                 3. On the 8086, a word is 16 bits.
  256.  
  257.  
  258.  
  259.  
  260.              Chapter 0.2 - Binary and Hex Numbers                          xix
  261.              ____________________________________
  262.  
  263.  
  264.  
  265.              What if it is a signed number? The first thing we need to know
  266.              about signed numbers is what is positive and what is negative.
  267.              Once again, for reasons of symmetry, we choose positive to be
  268.                  from 0000 0000 0000 0000 0000 0000 0000 0000
  269.                  to   0111 1111 1111 1111 1111 1111 1111 1111 
  270.                  (hex 00000000 to 7FFFFFFF) 
  271.              and we choose negative to be 
  272.                  from 1000 0000 0000 0000 0000 0000 0000 0000 
  273.                   to   1111 1111 1111 1111 1111 1111 1111 1111 
  274.                  (hex 10000000 to FFFFFFFF).{4}  
  275.              This longer number system cycles 
  276.                  from 1111 1111 1111 1111 1111 1111 1111 1111 
  277.                  to   0000 0000 0000 0000 0000 0000 0000 0000 
  278.                  (hex FFFFFFFF to 00000000). 
  279.              Notice that by using binary numbers we are innundating ourselves
  280.              with 1s and 0s.
  281.  
  282.  
  283.              If it is a positive signed number, it is still no problem (recall
  284.              that in our 16 bit system, a positive number is between 0000 0000
  285.              0000 0000 and 0111 1111 1111 1111, a negative signed number is
  286.              between 1000 0000 0000 0000 and 1111 1111 1111 1111). Just put 0s
  287.              to the left. Here are some positive signed numbers and their
  288.              conversions:
  289.  
  290.                 (1974)
  291.                 0000 0111 1011 0110 -> 0000 0000 0000 0000 0000 0111 1011 0110 
  292.                 (1)
  293.                 0000 0000 0000 0001 -> 0000 0000 0000 0000 0000 0000 0000 0001
  294.                 (3909)
  295.                 0000 1111 0100 0101 -> 0000 0000 0000 0000 0000 1111 0100 0101
  296.  
  297.              If it is a negative number, where did its representation come
  298.              from in our 16 bit system? -x -> 1111 1111 1111 1111 + 1 -x =
  299.              1111 1111 1111 1111 - x + 1. This time it won't be FFFFh + 1 but
  300.              FFFFFFFFh + 1. Let's have some examples. (Here we have 8 bits to
  301.              the group because there is not enough space on the line  to
  302.              accomodate 4 bits to the group).
  303.  
  304.  
  305.  
  306.                16 BIT SYSTEM                  32 BIT SYSTEM
  307.  
  308.               -1964
  309.              11111111 11111111 + 1     11111111 11111111 11111111 11111111 + 1
  310.              00000111 10101100         00000000 00000000 00000111 10101100  
  311.  
  312.              11111000 01010011 + 1     11111111 11111111 11111000 01010011 + 1
  313.  
  314.              11111000 01010100         11111111 11111111 11111000 01010100  
  315.              ____________________
  316.  
  317.                 4. Once again, the sign will be decided by the left hand
  318.              digit. If it is 0 it is a positive number; if it is 1 it is a
  319.              negative number.
  320.  
  321.  
  322.  
  323.  
  324.              The PC Assembler Tutor                                         xx
  325.              ______________________
  326.  
  327.  
  328.  
  329.  
  330.              -2867
  331.              11111111 11111111 + 1     11111111 11111111 11111111 11111111 + 1
  332.              00001011 00110011         00000000 00000000 00001011 00110011
  333.  
  334.              11110100 11001100 + 1     11111111 11111111 11110100 11001100 + 1
  335.  
  336.              11110100 11001101         11111111 11111111 11110100 11001101
  337.  
  338.  
  339.  
  340.              -182
  341.              11111111 11111111 + 1     11111111 11111111 11111111 11111111 + 1
  342.              00000000 10110110         00000000 00000000 00000000 10110110
  343.  
  344.              11111111 01001001 + 1     11111111 11111111 11111111 01001001 + 1
  345.  
  346.              11111111 01001010         11111111 11111111 11111111 01001010
  347.  
  348.              As you can see, all you need to do to sign extend a negative
  349.              number is to put 1s to the left. 
  350.  
  351.              Can't those 1s on the left become 0s when we add that 1 at the
  352.              end?  No. In order for that to happen, the right 16 bits must be
  353.              1111 1111 1111 1111. But that can only happen if the number to be
  354.              negated is 0:
  355.  
  356.                   1111 1111 1111 1111 1111 1111 1111 1111 + 1
  357.                                      -0000 0000 0000 0000
  358.                   1111 1111 1111 1111 1111 1111 1111 1111 + 1 -> 
  359.  
  360.                                      0000 0000 0000 0000 0000 0000 0000 0000
  361.  
  362.              In all other cases, adding 1 does not carry anything out of the
  363.              right 16 bits.
  364.  
  365.  
  366.              It is impossible to truncate one of these 32 bit numbers to a 16
  367.              bit number without making the results unreliable. Here are two
  368.              examples:
  369.  
  370.              +1,687,451     
  371.              00000000 00011001 10111111 10011011 -> 10111111 10011011 (-16485) 
  372.  
  373.              -3,524,830            
  374.              11111111 11001010 00110111 00100010 -> 00110111 00100010 (+14114)
  375.  
  376.              Truncating has changed both the sign and the absolute value of
  377.              the number.
  378.  
  379.  
  380.  
  381.  
  382.  
  383.  
  384.  
  385.  
  386.  
  387.                                                                            xxi
  388.  
  389.                                    CHAPTER 0.3 - LOGIC
  390.  
  391.  
  392.              Programs use numbers a lot. But they also ask questions that
  393.              require a yes/no answer. Is there an 8087 chip in the computer?
  394.              Is there a color monitor; how about a monochrome monitor? Is
  395.              there keyboard input waiting to be processed? Are you going to
  396.              get lucky on your date on Friday? Or, since you are a computer
  397.              programmer, are you going to have a date this month? Did the file
  398.              open correctly? Have we reached end of file?
  399.  
  400.              In order to combine these logical questions to our heart's
  401.              content, we need a few operations: AND, OR, XOR (exclusive or),
  402.              and NOT.
  403.  
  404.  
  405.              AND
  406.  
  407.              If we have two sentences "A" and "B", then ("A" AND "B") is true
  408.              if (and only if) both "A" and "B" are true. "It is raining and I
  409.              am wet" is true only if both "It is raining" and "I am wet" are
  410.              true. If one or both are false, then 'A and B' is false. A
  411.              shortcut for writing this is to use a truth table. A truth table
  412.              tells under what conditions an expression is true or false. All
  413.              we need to know is whether each component expression is true or
  414.              false. T stands for true, F for false.
  415.  
  416.                  "A"  "B"       "A" AND "B"
  417.                   
  418.                   T    T             T
  419.                   T    F             F
  420.                   F    T             F
  421.                   F    F             F
  422.  
  423.              Notice that the truth table does NOT say anything about whether
  424.              the expression makes sense. The sentence:
  425.  
  426.                  "It's hot and I am sweating."
  427.  
  428.              is a reasonable expression which may or may not be true. It will
  429.              be true if both "It is hot" and "I am sweating" are true. But the
  430.              sentence:
  431.  
  432.                  "The trees are green and Quito is the Capital of Ecuador."
  433.  
  434.              is pure garbage. It does not satisfy our idea of what a sensible
  435.              expression should be, and should NEVER be evaluated by means of a
  436.              truth table. The warranty on a truth table is, if the expression
  437.              makes sense, then the truth table will tell you under what
  438.              conditions it is true or false. If the expression does not make
  439.              sense, then the truth table tells you nothing. 
  440.  
  441.              Fortunately, this problem really belongs to philisophical logic.
  442.              When you use logical operators in your program, there will be a
  443.  
  444.              ______________________
  445.  
  446.              The PC Assembler Tutor - Copyright (C) 1989 Chuck Nelson
  447.  
  448.  
  449.  
  450.  
  451.              The PC Assembler Tutor                                       xxii
  452.              ______________________
  453.  
  454.              well defined reason for each use. If you start doing screwy
  455.              things, your program probably won't run. 
  456.  
  457.  
  458.              OR
  459.  
  460.              There are two different types of OR alive and kicking in the
  461.              English language - the exclusive OR (A or B but not both) and the
  462.              inclusive OR (A or B or both). 
  463.  
  464.              A mother tells her child "You can have a piece of cake or a piece
  465.              of candy." Does this mean that he can have both if he wants? Of
  466.              course not. He can have one or the other, but not both. This is
  467.              XOR, the exclusive or. The truth table for this is:
  468.  
  469.                  "A"  "B"       "A" XOR "B"
  470.                   
  471.                   T    T             F
  472.                   T    F             T
  473.                   F    T             T
  474.                   F    F             F
  475.  
  476.              'A XOR B' is true if exactly one of them is true. If they both
  477.              are true 'A XOR B' is false. If neither is true, 'A XOR B' is
  478.              false. Examples of XOR are:
  479.  
  480.                  1) We will either go to Italy or to Japan on our vacation.
  481.                  2) I'll either have a tuna salad or a chef's salad.
  482.                  3) He'll either buy a Lamborghini or a BMW.
  483.  
  484.  
  485.              Consider this sentence: "To go to Harvard, you need to have
  486.              connections or to be very smart." Do we want this to mean that if
  487.              you have connections but are very smart, you are automatically
  488.              excluded from going to Harvard?  No. We want this to mean one or
  489.              the other or perhaps both. Sometimes you write this as 'and/or'
  490.              if you want to be absolutely clear. This is the inclusive OR. The
  491.              truth table for OR is:
  492.  
  493.                  "A"  "B"       "A" OR "B"
  494.                   
  495.                   T    T             T
  496.                   T    F             T
  497.                   F    T             T
  498.                   F    F             F
  499.  
  500.              'A OR B' is true if one or both of them are true. If both are
  501.              false, then it is false. Examples of OR are:
  502.  
  503.                  1) They'll either go to Italy or to Austria on their   
  504.                  vacation.
  505.                  2) I'll have either steak or shrimp at The Sizzler.
  506.                  3) He'll buy either a paisley tie or a rep tie.
  507.  
  508.              The three sentences for XOR and OR mimic each other on purpose.
  509.              In the English language, you know which type of OR is being used
  510.              by what is being talked about. You know intuitively which one
  511.  
  512.  
  513.  
  514.  
  515.              Chapter 0.3 - Logic                                         xxiii
  516.              ___________________
  517.  
  518.              applies. If someone buys two different ties you are not suprised.
  519.              If someone buys two expensive cars at the same time you are quite
  520.              surprised.{1}  With very few exceptions, if you confuse the two
  521.              you are doing it on purpose. If your father says "You can have
  522.              the car on Friday night or on Saturday night." and you don't
  523.              understand which OR applies, it's not his fault.
  524.  
  525.  
  526.              NOT
  527.  
  528.              The final logical operation is NOT. The sentence: "It is not
  529.              raining." is false if it is raining, and true otherwise. The
  530.              truth table is:
  531.  
  532.                  "A"    NOT "A"
  533.  
  534.                   T        F
  535.                   F        T
  536.  
  537.              This is self-explanatory.
  538.  
  539.              Amazingly enough, this is all you need to know about logic to be
  540.              a quality programmer. Trying to make very complex combinations of
  541.              these logical operations may be fun for philosophy, but it is
  542.              death to a program. KISS is the operative word in programming.{2}
  543.  
  544.  
  545.  
  546.  
  547.  
  548.  
  549.  
  550.  
  551.  
  552.  
  553.  
  554.  
  555.  
  556.  
  557.  
  558.  
  559.  
  560.  
  561.  
  562.  
  563.  
  564.  
  565.  
  566.  
  567.  
  568.  
  569.              ____________________
  570.  
  571.                 1. Especially if his job is working the cash register at
  572.              Sears.
  573.  
  574.                 2. Which, if you don't know, is Keep It Simple, Stupid.
  575.  
  576.  
  577.  
  578.  
  579.                                                                           xxiv
  580.  
  581.                                  CHAPTER 0.4 - MEMORY
  582.  
  583.  
  584.              The basic unit of memory on 8086 machines is the byte.{1} This
  585.              means that in every memory cell we can store a number from 0 to
  586.              255. Each memory cell has an address. The first one in memory has
  587.              address 0, then address 1, then 2, then 3, etc.
  588.  
  589.              The registers on the 8086 are one word (two bytes) long. This
  590.              means that any register can store and operate on a number from 0
  591.              to 65,535. (It also has some registers which can operate on bytes
  592.              and can store and operate on numbers from 0 to 255.)
  593.  
  594.              Memory is physically external to the 8086. Registers are
  595.              physically internal to the 8086; they are actually on the chip. 
  596.  
  597.              One of the ways that we can access memory on the 8086 is by
  598.              putting the address of a memory cell in a register and then
  599.              telling the 8086 that we want to use the data at that memory
  600.              address.
  601.  
  602.              Since each byte has its own address, and since we can't have a
  603.              number larger that 65,535 in any one register, it is impossible
  604.              to address more than 65,535 bytes with a single register. Back in
  605.              the dark ages, that might have been enough memory, but it sure
  606.              isn't enough nowdays.
  607.  
  608.              Intel solved the problem by creating SEGMENTS. Each segment is
  609.              65,536 bytes long, going from address 0 to address 65,535.{2} You
  610.              tell the 8086 where you want to go in memory by telling it which
  611.              segment you are in and what the address is within that segment.
  612.              Segments are numbered from 0 to 65,535. That is, there are 65,536
  613.              of them. 
  614.  
  615.              As a design decision, Intel decided that a segment should start
  616.              every 16 bytes. This decision was made in the late 70's and is
  617.              cast in stone. On the 8086, a segment starts every 16 bytes. Here
  618.              is a list of some segments, with the segment number and the
  619.              segment starting address in both decimal and hex.
  620.  
  621.  
  622.                       SEGMENT NUMBER                   STARTING ADDRESS
  623.  
  624.                        0d        0h                     0d          00h
  625.                        1d        1h                    16d          10h
  626.                        2d        2h                    32d          20h
  627.                        3d        3h                    48d          30h
  628.                        4d        4h                    64d          40h
  629.              ____________________
  630.  
  631.                 1. As it is on all computers.
  632.  
  633.                 2. The last segments are actually less that 65,535, as will be
  634.              explained later.
  635.  
  636.              ______________________
  637.  
  638.              The PC Assembler Tutor - Copyright (C) 1989 Chuck Nelson
  639.  
  640.  
  641.  
  642.  
  643.              Chapter 0.4 - Memory                                          xxv
  644.              ____________________
  645.  
  646.                      200d       C8h                 3,200d         C80h
  647.                    21694d     54BEh               347,104d       54BE0h
  648.                    51377d     C8B1h               822,032d       C8B10h
  649.  
  650.  
  651.              One thing you should note is that in hex, the segment number is
  652.              the same as the starting address, except that the starting
  653.              address has an extra 0 digit on the right.
  654.  
  655.              These segments overlap. A new segment starts every 16 bytes, and
  656.              they are all 65,535 bytes long. This means that with the
  657.              exception of the first 16 bytes, any address in memory is in more
  658.              that one segment. Here are some addresses. The word "offset"
  659.              means the number of bytes from the beginning of the segment. (It
  660.              is possible for a memory cell to have an offset 0). The offset is
  661.              shown in both decimal (d) and hex (h):
  662.  
  663.              memory address  55    (37h)
  664.  
  665.                          Seg #        Offset        Offset  
  666.                             0            55d          37h
  667.                             1            39d          27h
  668.                             2            23d          17h
  669.                             3             7d           7h
  670.  
  671.              Thus the address 55 is in 4 different segments, and can be
  672.              addressed relative to any one of them.
  673.  
  674.              memory address  17,946   (461Ah)
  675.  
  676.                         Seg #         Offset         Offset
  677.                             0        17,946d          461Ah
  678.                             1        17,930d          460Ah
  679.                             2        17,914d          45FAh
  680.                           ...            ...
  681.                          1120            26d            1Ah
  682.                          1121            10d            0Ah 
  683.  
  684.              The address 17,946 is in 1122 different segments, and can be
  685.              addressed relative to any of them. Notice that as the segment
  686.              number goes up one segment, the offset address goes down 16 bytes
  687.              (10h).
  688.  
  689.              Above the address 65,519, every memory cell can be addressed by
  690.              4,096 different segments.
  691.  
  692.              Because of the way the addresses are generated on the 8086, the
  693.              maximum memory address possible is 1,048,575 (FFFFF hex.) This
  694.              means that the final segments are less than 65,536 bytes long. In
  695.              this table "Address" is the starting address of the segment. All
  696.              the following numbers are decimal:
  697.  
  698.                     Segment #        Address           Max Offset
  699.                       65,535d     1,048,560d                  15d
  700.                       65,534d     1,048,544d                  31d
  701.                       65,533d     1,048,528d                  47d
  702.                           ...            ...                  ...
  703.  
  704.  
  705.  
  706.  
  707.              The PC Assembler Tutor                                       xxvi
  708.              ______________________
  709.  
  710.                       64,000d     1,024,000d              24,575d
  711.  
  712.              Let's look at these same numbers in hex:
  713.  
  714.                     Segment #        Address           Max Offset
  715.                         FFFFh         FFFF0h                   Fh
  716.                         FFFEh         FFFE0h                  1Fh
  717.                         FFFDh         FFFD0h                  2Fh
  718.                           ...            ...                  ...
  719.                         FA00h         FA000h                5FFFh
  720.  
  721.              The maximum addressable number is FFFFFh, which is why these
  722.              segments are cut short. There are 4,095 segments at the top which
  723.              are less than 65,536 bytes long. Don't worry, though, because
  724.              this top section of memory belongs to the operating system, and
  725.              your programs will never be put there. It will never affect you.
  726.  
  727.              Back in the late 70s, a million bytes of memory seemed like a
  728.              lot. In the 60s, large mainframe computers had only a
  729.              half-million bytes of memory. In the 70s memory was still
  730.              exhorbitantly expensive. Nowdays, however, you practically need a
  731.              megabyte just to blow your nose. But this segmentation system is
  732.              cast in stone, so there is no way to get more memory on the
  733.              8086.{3}
  734.  
  735.              In the beginning, when we make a program, we will use one segment
  736.              for the machine code, one segment for permanant data, and one
  737.              segment for temporary data. If we need it, this gives us 196,608
  738.              bytes of usable memory right off the bat. As you will see by the
  739.              time we are finished, ALL memory is addressable - we just need to
  740.              do more work to get to it all.
  741.  
  742.              All this talk about segments and offsets may have you concerned.
  743.              If you have to keep track of all these offsets, programming is
  744.              going to be very difficult.{4}  Not to worry. It is the
  745.              assembler's job to keep track of the offsets of every variable
  746.  
  747.  
  748.  
  749.  
  750.              ____________________
  751.  
  752.                 3. This megabyte rule is unalterable. EMS (extended memory) is
  753.              actually a memory swapping program. On the 28086 and 80386 you
  754.              can have more than one megabyte of memory but the program can
  755.              only access one megabyte. The program reserves a section of its
  756.              one megabyte for a transfer area. It then calls EMS which
  757.              transfers the data from this extended memory to the transfer
  758.              area. It is in effect a RAM disk. It is like using a hard disk
  759.              but is much faster. If Intel had bitten the bullet with the 80286
  760.              and said that a segment would start every 256 bytes instead of
  761.              every 16 bytes, we would have 16 megabytes of directly accessible
  762.              memory instead of 1 megabyte. Hindsight is such a wonderful
  763.              thing.
  764.  
  765.                 4. Remember, an offset is just how many bytes a memory cell is
  766.              from the beginning of the segment.
  767.  
  768.  
  769.  
  770.  
  771.              Chapter 0.4 - Memory                                        xxvii
  772.              ____________________
  773.  
  774.              and every label in your program.{5} 
  775.  
  776.              Which segments your program uses is decided by the operating
  777.              system when it loads your program into memory. It puts some of
  778.              this information into the 8086. At the start of the program, you
  779.              put the rest of the information into the 8086, and then you can
  780.              forget about segments.
  781.  
  782.  
  783.              NUMBERS IN MEMORY
  784.  
  785.              The largest number you can store in a single byte is 255. If you
  786.              are calculating the distance from the sun to Alpha Centauri in
  787.              inches, obviously one byte is not enough. Unfortunately, the 8086
  788.              can't really handle large numbers like that.{6} It can handle
  789.              numbers which are 16 bits (2 bytes) long. However, with
  790.              subprograms supplied with all compilers, we can handle large
  791.              numbers, though rather slowly if we don't use an 8087. All these
  792.              different programs need a standard way to write numbers in
  793.              memory, and this standard is supplied by Intel. The standard is :
  794.  
  795.                (1)  integers can be 1, 2, or 4 bytes long. This corresponds
  796.                to -128 to +127 , -32,768 to +32,767, and -2,147,483,648 to
  797.                +2,147,483,647. 
  798.  
  799.                (2)  scientific floating point numbers which have an exponent
  800.                and can be very large. They come as 4 byte and 8 byte numbers.
  801.                We will not deal with them at all, but we need to know how
  802.                they are stored.
  803.  
  804.                (3)  Commercial or BCD numbers which occupy 1/2 byte per
  805.                digit. Since some of the 8086 instructions are concerned with
  806.                these we will cover them, but if you are not curious about
  807.                them, you can skip that section. The standard is a 10 byte
  808.                number.
  809.  
  810.              Let's look at a number. For the rest of this section, all numbers
  811.              will be hex, and if a number is longer than one byte, we will
  812.              display it with a blank space between each byte. If it is a one
  813.              byte number - e.g. 3C, we know exactly where we are going to put
  814.              it. But what if a number is 4 bytes long - e.g. 2D F5 33 0A - and
  815.              we want to put it in memory starting at offset 264. We have two
  816.              choices:
  817.  
  818.              2D F5 33 0A
  819.                     Address          Choice 1     Choice 2
  820.                       267               2D           0A
  821.                       266               F5           33
  822.                       265               33           F5
  823.              ____________________
  824.  
  825.                 5. As in other languages, a label is a name that marks a place
  826.              in the code. In BASIC, labels are actually numbers (such as 500
  827.              in the instruction GOTO 500). Labels are frowned on in Pascal and
  828.              C, but are the lifeblood of assembler language.
  829.  
  830.                 6. But fortunately, the 8087 can.
  831.  
  832.  
  833.  
  834.  
  835.              The PC Assembler Tutor                                     xxviii
  836.              ______________________
  837.  
  838.                       264               0A           2D
  839.  
  840.              Neither choice is better than the other. Choice 1 puts the
  841.              right-most byte in low memory, choice 2 puts the right-most byte
  842.              in high memory.{7} The right-most byte is called the LEAST
  843.              SIGNIFICANT BYTE because it has the least effect on a number,
  844.              while the left-most byte is called the MOST SIGNIFICANT BYTE
  845.              because it has the most effect on a number. In fact, Intel picked
  846.              choice #1 for the 8086 (which has the least significant byte in
  847.              low memory), and Motorola picked choice #2 for the 68000 (which
  848.              has the most significant byte in low memory). 
  849.  
  850.              This is consistent for both the 8086 and the 8087: THE LEAST
  851.              SIGNIFICANT BYTE IS ALWAYS IN LOW MEMORY: EACH NUMBER IN MEMORY
  852.              STARTS WITH THE LEAST SIGNIFICANT BYTE. Remember that, and you'll
  853.              save yourself some trouble. 
  854.  
  855.              One problem you will run up against is that when we draw pictures
  856.              of memory, we often draw from left to right, that is:
  857.  
  858.                ADDRESS        264  265  266  267
  859.  
  860.              When we do that, things start looking wierd. For 2D F5 33 0A we
  861.              have:
  862.  
  863.                ADDRESS        264  265  266  267
  864.                VALUE          0A   33   F5   2D
  865.  
  866.              This is exactly backwards. Remember, memory doesn't go from left
  867.              to right, it goes UP from 0, and THE LEAST SIGNIFICANT BYTE IS
  868.              ALWAYS IN LOW MEMORY. You will certainly make some mistakes till
  869.              you get used to being consistent about this. The right hand digit
  870.              of a number is always in low memory. If you think of memory as
  871.              being VERTICAL:
  872.  
  873.              1E A3 07 B5
  874.                             Value       Address
  875.                               1E         4782
  876.                               A3         4781
  877.                               07         4780
  878.                               B5         4779
  879.  
  880.              rather than being LEFT TO RIGHT:
  881.  
  882.                Address   4779 4780 4781 4782
  883.                Value      B5   07   A3   1E
  884.  
  885.              you will be much better off.
  886.  
  887.  
  888.  
  889.  
  890.  
  891.              ____________________
  892.  
  893.                 7. Low memory always means the low addresses and high memory
  894.              always means the high addresses.
  895.  
  896.  
  897.  
  898.  
  899.                                                                           xxix
  900.  
  901.                                  CHAPTER 0.5 - STYLE
  902.  
  903.  
  904.              Finally, it is time to say a word about style. Assembler code is
  905.              by its nature difficult to read. It divides any concept into a
  906.              number of sequential steps. If you have the BASIC statement:
  907.  
  908.                  MINUTES = DAYS * 1440
  909.  
  910.              You get the idea because you can scan the line to see what is
  911.              wanted. The assembler code for the above line is: {1}
  912.  
  913.                  mov  ax, days
  914.                  mov  bx, 1440
  915.                  mul  bx
  916.                  mov  minutes, ax
  917.  
  918.              In BASIC, the concept was imbedded in the expression. In
  919.              assembler it was lost. This means two things. First, you must be
  920.              religious about documenting every step. If you come back to
  921.              something two or three months later and you haven't documented
  922.              what you are doing, it may take you longer to figure out what you
  923.              did than it would to completely rewrite what you did. 
  924.  
  925.              Secondly, if you are a person who likes code like this:
  926.  
  927.                  x = (y + k) / (z - 4)
  928.  
  929.              you are headed for big trouble. At the assembler level it is
  930.              CRITICAL that you give every variable a name that signifies
  931.              exactly what it is. If you are counting the number of correct
  932.              answers, then the variable should be:
  933.  
  934.                  correct_answers
  935.  
  936.              not 'K'. If you are looking at the remainder from a division,
  937.              then the variable should be:
  938.  
  939.                  remainder
  940.  
  941.              not 'R'. If you try to use short names like 'x', 'k' and 'y', I
  942.              will guarantee that for every minute you save by not having to
  943.              type in long variable names, you will lose 10 minutes by not
  944.              being able to figure out what is going on when you reread the
  945.              code. In this tutorial we will use multiple words connected by
  946.              underscores:
  947.  
  948.                  first_positive_prime
  949.                  median_age
  950.                  oldest_student
  951.              ____________________
  952.  
  953.                 1. Don't worry about what this code does. You will learn soon
  954.              enough.
  955.  
  956.              ______________________
  957.  
  958.              The PC Assembler Tutor - Copyright (C) 1989 Chuck Nelson
  959.  
  960.  
  961.  
  962.  
  963.              The PC Assembler Tutor                                        xxx
  964.              ______________________
  965.  
  966.  
  967.              The Microsoft assembler allows 31 significant characters in a
  968.              name. Even though there are several other characters allowed, we
  969.              will use only letters, the underscore, and numbers (where
  970.              appropriate):
  971.  
  972.                  approximation1
  973.                  approximation2
  974.                  approximation3
  975.  
  976.              This should make your code similar to well written C or Pascal
  977.              code, and greatly increase the readability of the code.
  978.  
  979.  
  980.